package com.freight.base.utils;

import com.freight.base.service.AreaService;
import com.freight.base.vo.AreaAddressVo;
import com.freight.base.vo.AreaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;


@Component
public class AreaAddressUtil<T extends AreaAddressVo> {

    @Autowired
    AreaService areaService;

    public <T extends AreaAddressVo> List<T> fillAreaAddress(List<T> records) {
        HashSet<String> searchAreas = new HashSet<>();
        for (T record : records) {
            if (record.getBeginAreaCode() != null && record.getBeginAreaCode().split(",").length > 0) {
                searchAreas.addAll(Arrays.asList(record.getBeginAreaCode().split(",")));
            }
            if (record.getEndAreaCode() != null && record.getEndAreaCode().split(",").length > 0) {
                searchAreas.addAll(Arrays.asList(record.getEndAreaCode().split(",")));
            }
        }

        HashMap<String, AreaVo> areaMap = areaService.getAreaVoLevelMap(new ArrayList<>(searchAreas));

        for (T record : records) {

            AreaVo begin = record.getBeginAreaCode() != null ? areaMap.get(record.getBeginAreaCode().split(",")[0]) : null;
            AreaVo end = record.getEndAreaCode() != null ? areaMap.get(record.getEndAreaCode().split(",")[0]) : null;
            if (begin != null) {
                record.setBeginCityName(begin.getCityName());
                record.setBeginCityCode(begin.getCityCode());
                record.setBeginAreaName(begin.getAreaName());
                record.setBeginProvinceCode(begin.getProvinceCode());
                record.setBeginProvinceName(begin.getProvinceName());
            } else {
                record.setBeginCityName("未知");
                record.setBeginCityCode("-911");
                record.setBeginAreaName("未知");
                record.setBeginProvinceCode("-911");
                record.setBeginProvinceName("未知");
            }
            if (end != null) {
                record.setEndProvinceCode(end.getProvinceCode());
                record.setEndCityName(end.getCityName());
                record.setEndCityCode(end.getCityCode());
                record.setEndAreaName(end.getAreaName());
                record.setEndProvinceName(end.getProvinceName());
            } else {
                record.setEndCityName("未知");
                record.setEndCityCode("-911");
                record.setEndAreaName("未知");
                record.setEndProvinceName("未知");
            }

        }
        return records;

    }

    public <T extends AreaAddressVo> T fillAreaAddress(T record) {

        HashSet<String> searchAreas = new HashSet<>();

        if (record.getBeginAreaCode() != null && record.getBeginAreaCode().split(",").length > 0) {
            searchAreas.addAll(Arrays.asList(record.getBeginAreaCode().split(",")));
        }

        if (record.getEndAreaCode() != null && record.getEndAreaCode().split(",").length > 0) {
            searchAreas.addAll(Arrays.asList(record.getEndAreaCode().split(",")));
        }

        HashMap<String, AreaVo> areaMap = areaService.getAreaVoLevelMap(new ArrayList<>(searchAreas));
        AreaVo begin = record.getBeginAreaCode() != null ? areaMap.get(record.getBeginAreaCode().split(",")[0]) : null;
        AreaVo end = record.getEndAreaCode() != null ? areaMap.get(record.getEndAreaCode().split(",")[0]) : null;

        if (begin != null) {
            record.setBeginCityName(begin.getCityName());
            record.setBeginAreaName(begin.getAreaName());
            record.setBeginCityCode(begin.getCityCode());
            record.setBeginProvinceCode(begin.getProvinceCode());
            record.setBeginProvinceName(begin.getProvinceName());
        } else {
            record.setBeginCityName("未知");
            record.setBeginAreaName("未知");
            record.setBeginCityCode("-911");
            record.setBeginProvinceName("未知");
        }
        if (end != null) {
            record.setEndProvinceCode(end.getProvinceCode());
            record.setEndCityName(end.getCityName());
            record.setEndAreaName(end.getAreaName());
            record.setEndCityCode(end.getCityCode());
            record.setEndProvinceName(end.getProvinceName());
        } else {
            record.setEndCityName("未知");
            record.setEndAreaName("未知");
            record.setEndCityCode("-911");
            record.setEndProvinceName("未知");
        }

        return record;

    }


}
